package com.rj.data.keywordjob;


import com.google.common.base.Joiner;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor;
import com.hankcs.hanlp.seg.common.Term;
import com.hankcs.hanlp.summary.TextRankKeyword;
import com.hankcs.hanlp.tokenizer.IndexTokenizer;
import com.hankcs.hanlp.tokenizer.NLPTokenizer;
import com.hankcs.hanlp.tokenizer.StandardTokenizer;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * NLP企业名称keyword提取
 */
public class KeywordFetcher {
    public static void main(String[] args) throws IOException {


        List<String> resLines = Files.asCharSource(new File("D:\\data\\cbname.txt"), Charset.forName("UTF-8")).readLines(new LineProcessor<List<String>>() {
            List<String> tlines = new ArrayList<>();

            @Override
            public boolean processLine(String line) throws IOException {
                List<Term> segments = NLPTokenizer.segment(line);
                String lineTerm = line + "|" + Joiner.on(",").join(segments);
                tlines.add(lineTerm);
                return true;
            }

            @Override
            public List<String> getResult() {
                return tlines;
            }
        });


        try {
            FileUtils.writeLines(new File("D:\\data\\keywords_3"), resLines, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void analyseName(String cname) {
        TextRankKeyword textRankKeyword = new TextRankKeyword();
//        Map<String, Float> ranks = textRankKeyword.getTermAndRank(cname);
        List<String> trms = textRankKeyword.getKeywords(cname, 1000000);
//        for (Term trm : trms) {
//            System.out.println(trm.toString());
//        }
//        List<String> lists = new ArrayList<>();
//        ranks.entrySet().forEach(en -> {
//            {
//                lists.add(en.getKey() + "," + en.getValue());
//                System.out.println(en.getKey() + "," + en.getValue());
//            }
//        });
        try {
            FileUtils.writeLines(new File("D:\\data\\keywords_2"), trms, true);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void nlpName(String cname, List<String> tlines) {


    }
}
